package com.learning.dubbo.service.mock.consumer;

import com.learning.dubbo.service.mock.api.GreetingsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * ClassName: Consumer
 * Description: 消费客户端
 * Date: 2020/6/15 13:43 【需求编号】
 *
 * @author Sam Sho
 * @version V1.0.0
 */
@Slf4j
public class Consumer {
    public static void main(String[] args) throws Exception {
//        consumer();
        consumerRetry();
    }

    private static void consumer() {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml");
        context.start();

        // 获取远程服务代理
        GreetingsService greetingsService = (GreetingsService) context.getBean("greetingsService");
        // 执行远程方法
        String result = greetingsService.sayHi("Dubbo");
        // 显示调用结果
        log.info("~~~~~~~~~~~ {} ~~~~~~~~~~~~~", result);
        // 代理对象
        log.info(greetingsService.getClass().getName());


        greetingsService = (GreetingsService) context.getBean("greetingsService2");
        System.out.println(greetingsService.sayHi("Dubbo"));

        greetingsService = (GreetingsService) context.getBean("greetingsService3");
        System.out.println(greetingsService.sayHi("Dubbo"));


        greetingsService = (GreetingsService) context.getBean("greetingsService4");
        System.out.println(greetingsService.sayHi("Dubbo"));


        greetingsService = (GreetingsService) context.getBean("greetingsService5");
        System.out.println(greetingsService.sayHi("Dubbo"));
        context.close();
    }

    /**
     * 重试与mock一起使用：
     * 会先重试，然后出发mock
     */
    private static void consumerRetry() {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml");
        context.start();

        // 获取远程服务代理
        GreetingsService greetingsService = (GreetingsService) context.getBean("greetingsService");
        // 执行远程方法
        String result = greetingsService.sayHello("Dubbo");
        // 显示调用结果
        log.info("~~~~~~~~~~~ {} ~~~~~~~~~~~~~", result);
        // 代理对象
        log.info(greetingsService.getClass().getName());
        context.close();
    }
}
